<?php
	require("libraries/student.php");
	class Hottels extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->helper(array("url","form"));
			$this->load->library("form_validation");
			$this->load->library("string");	
			$this->load->model("model_hottel");	
			$this->load->model("model_tours");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/hottels/index/';
			$config['total_rows'] = $this->model_hottel->count_all();
			$data['title'] = "Manage hottels";
			$data['template'] = "hottels/list_hottel";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 5;
			$data['listall']= $this->model_hottel->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function add(){
		  	$data['title'] 		= "Add hottel";
			$data['template'] 	= "hottels/add_hottel";
			$data['data']['category'] = $this->model_tours->listcate();
			$data['act'] = 5;
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("cate_id","Select category","required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					$frienly = str_replace(' ', '-', strtolower($this->string->replace($this->input->post('hottel_name'))));
					$db = array(
						"hottel_name" => $this->input->post("hottel_name"),
						"hottel_name_rewrite" => $frienly,
						"hottel_code"  => $this->input->post("hottel_code"),
						"hottel_star"  => $this->input->post("hottel_star"),
						"hottel_short_info"  => $this->input->post("hottel_short_info"),
						"hottel_phone" => $this->input->post("hottel_phone"),
						"hottel_add"  => $this->input->post("hottel_add"),
						"hottel_price" => $this->input->post("hottel_price"),
                        "hottel_info"  => $this->input->post("hottel_info"),
						"hottel_status" => "1",
						"hottel_show" => $this->input->post("hottel_show"),
						"cate_id"     => $this->input->post("cate_id"),
					);
					if($_FILES['hottel_image1']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image1")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add hottel";
							$data['template'] 	= "hottels/add_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image1'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image1'],"hottels",400,300);
						}
					}
					if($_FILES['hottel_image2']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image2")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add hottel";
							$data['template'] 	= "hottels/add_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image2'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image2'],"hottels",400,300);
						}
					}
					if($_FILES['hottel_image3']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image3")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add hottel";
							$data['template'] 	= "hottels/add_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image3'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image3'],"hottels",400,300);
						}
					}
					if($_FILES['hottel_image4']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image4")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add hottel";
							$data['template'] 	= "hottels/add_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image4'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image4'],"hottels",400,300);
						}
					}
					$this->model_hottel->add($db);
					redirect(base_url()."admin/hottels ");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
		  	$data['title'] 		= "Edit hottel";
			$data['template'] 	= "hottels/edit_hottel";
			$data['data']['category'] = $this->model_tours->listcate();
			$data['act'] = 5;
			$data['getdata'] 	=  $this->model_hottel->getdata($id);
			if($data['getdata'] == NULL){
				die();
			}
			$data['stt'] = $data['getdata']['cate_id'];
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("cate_id","Select category","required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					$frienly = str_replace(' ', '-', strtolower($this->string->replace($this->input->post('hottel_name'))));
					$db = array(
						"hottel_name" => $this->input->post("hottel_name"),
						"hottel_name_rewrite" => $frienly,
						"hottel_code"  => $this->input->post("hottel_code"),
						"hottel_star"  => $this->input->post("hottel_star"),
						"hottel_short_info"  => $this->input->post("hottel_short_info"),
						"hottel_phone" => $this->input->post("hottel_phone"),
						"hottel_add"  => $this->input->post("hottel_add"),
						"hottel_price" => $this->input->post("hottel_price"),
                        "hottel_info"  => $this->input->post("hottel_info"),
						"hottel_status" => "1",
						"hottel_show" => $this->input->post("hottel_show"),
						"cate_id"     => $this->input->post("cate_id"),
					);
					if($_FILES['hottel_image1']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image1")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit hottel";
							$data['template'] 	= "hottels/edit_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$data['getdata'] 	=  $this->model_hottel->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image1'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image1'],"hottels",400,300);
						}
					}
					if($_FILES['hottel_image2']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image2")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit hottel";
							$data['template'] 	= "hottels/edit_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$data['getdata'] 	=  $this->model_hottel->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image2'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image2'],"hottels",400,300);
						}
					}
					if($_FILES['hottel_image3']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image3")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit hottel";
							$data['template'] 	= "hottels/edit_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$data['getdata'] 	=  $this->model_hottel->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image3'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image3'],"hottels",400,300);
						}
					}
					if($_FILES['hottel_image4']['name'] != NULL){
						$config['upload_path']   = './uploads/hottels';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("hottel_image4")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit hottel";
							$data['template'] 	= "hottels/edit_hottel";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 5;
							$data['getdata'] 	=  $this->model_hottel->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['hottel_image4'] = $data['file_name'];
							$this->createThumbnail($db['hottel_image4'],"hottels",400,300);
						}
					}
					$this->model_hottel->update($db,$id);
					redirect(base_url()."admin/hottels");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_hottel->getdata($id);
			@unlink("uploads/hottels/".$data['hottel_image1']);
			@unlink("uploads/hottels/thumb/".$data['hottel_image1']);
			@unlink("uploads/hottels/".$data['hottel_image2']);
			@unlink("uploads/hottels/thumb/".$data['hottel_image2']);
			@unlink("uploads/hottels/".$data['hottel_image3']);
			@unlink("uploads/hottels/thumb/".$data['hottel_image3']);
			@unlink("uploads/hottels/".$data['hottel_image4']);
			@unlink("uploads/hottels/thumb/".$data['hottel_image4']);
			$this->model_hottel->del($id);
			redirect(base_url()."admin/hottels");
		}
		public function createThumbnail($fileName,$path,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
			$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}